package io.renren.modules.hos.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import io.renren.common.utils.ShiroUtils;
import io.renren.modules.hos.vo.ShopCarVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.hos.entity.ShopCarEntity;
import io.renren.modules.hos.service.ShopCarService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author ryf
 * @email 2559228480@qq.com
 * @date 2021-12-22 21:50:18
 */
@RestController
@RequestMapping("hos/shopCar")
public class ShopCarController {
    @Autowired
    private ShopCarService shopCarService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("hos:shopcar:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = shopCarService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("hos:shopcar:info")
    public R info(@PathVariable("id") Long id){
		ShopCarEntity shopCar = shopCarService.getById(id);

        return R.ok().put("shopCar", shopCar);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("hos:shopcar:save")
    public R save(@RequestBody ShopCarEntity shopCar){
		shopCarService.save(shopCar);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("hos:shopcar:update")
    public R update(@RequestBody ShopCarEntity shopCar){
		shopCarService.updateById(shopCar);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("hos:shopcar:delete")
    public R delete(@RequestBody Long[] ids){
		shopCarService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 查询购物车内的药品
     */
    @RequestMapping("/shopCarList")
    public R queryByUser(){
        List<ShopCarVo> shopCarVos = shopCarService.queryByUser(ShiroUtils.getHosUserId());
        return R.ok().put("data", shopCarVos);
    }

    /**
     * 添加药品
     */
    @PostMapping("/add/{id}/{num}")
    public R addMedicine(@PathVariable("id") Long medicineId, @PathVariable("num") Integer num){
        String result = shopCarService.addMedicine(medicineId, num);
        return R.ok().put("data", result);
    }


}
